import router from "@/router"
import { useBreadCrumb } from "../store/breadCrumb"
import { usePermission } from "../store/permission"
// setTimeout(()=>{
//     let permissionStore = usePermission()

//  permissionStore.fetch_menulist()
// },2000)

//前置守卫钩子
router.beforeEach(async (to, from) => {
    let permissionStore = usePermission()

    // api.getMenuApi()
    // .then(res =>{
    //     console.log(res);
    // })
    //判断本地的sessionStorage是否有token，如果有，啥也不干
    let token = sessionStorage.getItem("my-token")
    if (!token) {
        //如果没有
        //判断，如果访问的不是登入页，强行跳转到登入页
        if (to.path !== "/login") {
            return {
                path: "/login"
            }
        }
    } else {
        //如果menulist的长度是0，那么就要发起请求
        if (permissionStore.menulist.length === 0) {
            //调用store中的action方法发起异步请求
            await permissionStore.fetch_menulist()
            //废弃要进入的路由，重新进入动态添加的路由
            return {
                ...to
            }
        }

    }
})
//后置钩子

router.afterEach((to, from) => {
 

    // console.log(to);
    let breadCrumbStore = useBreadCrumb()
    breadCrumbStore.$patch(state => {
        state.datas = to.matched
    })
})
